﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace eCompany.Forms
{
    public partial class currency_calc_form : Form
    {
        private eCompany.eObj.currency_list m_cl = null;

        public currency_calc_form(eCompany.eObj.currency_list cl, string default_cur)
        {
            InitializeComponent();
            this.Icon = pics.eCompanyIcon;

            m_cl = cl;
            InitCurrencyCB(default_cur);
            toLabel1.Text = "0 " + toComboBoxExtended1.Text;
            toLabel2.Text = "0 " + toComboBoxExtended2.Text;
            toLabel3.Text = "0 " + toComboBoxExtended3.Text;
        }

        private void InitCurrencyCB(string default_cur)
        {
            fromComboBoxExtended1.ItemHeight = currencyImageList.ImageSize.Height;
            fromComboBoxExtended2.ItemHeight = currencyImageList.ImageSize.Height;
            fromComboBoxExtended3.ItemHeight = currencyImageList.ImageSize.Height;
            toComboBoxExtended1.ItemHeight = currencyImageList.ImageSize.Height;
            toComboBoxExtended2.ItemHeight = currencyImageList.ImageSize.Height;
            toComboBoxExtended3.ItemHeight = currencyImageList.ImageSize.Height;

            foreach (eCompany.eObj.currency cur in m_cl.currencies)
            {
                Image img = Tools.GetCurrencyImage(cur.short_name);
                if (img != null)
                    currencyImageList.Images.Add(cur.short_name,img);

                ComboBoxExtendedItem item = new ComboBoxExtendedItem();
                item.ImageTag = cur.short_name;
                item.Obj = cur.short_name;
                fromComboBoxExtended1.Items.Add(item);
                fromComboBoxExtended2.Items.Add(item);
                fromComboBoxExtended3.Items.Add(item);
                toComboBoxExtended1.Items.Add(item);
                toComboBoxExtended2.Items.Add(item);
                toComboBoxExtended3.Items.Add(item);
            }

            fromComboBoxExtended1.Text = default_cur;
            fromComboBoxExtended2.Text = default_cur;
            fromComboBoxExtended3.Text = default_cur;
            toComboBoxExtended1.Text = "GLD";
            toComboBoxExtended2.Text = "GLD";
            toComboBoxExtended3.Text = "GLD";
        }

        private void convertValue(TextBox fromTB, ComboBoxExtended fromCB, ComboBoxExtended toCB, Label toLabel)
        {
            string sVal = fromTB.Text;
            double dVal = 0;
            System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo("en-US");
            sVal = sVal.Replace(",", ".");
            if (double.TryParse(sVal, System.Globalization.NumberStyles.AllowDecimalPoint, culture, out dVal) == true)
            {
                eCompany.eManager.eManager eM = new eManager.eManager();
                dVal = eM.currencyToGOLD(m_cl, fromCB.Text, dVal);
                if (toCB.Text != "GLD")
                    dVal = dVal / eM.currencyToGOLD(m_cl, toCB.Text, 1);
                toLabel.Text = eManager.eManager.formatDouble(dVal,4) + " " + toCB.Text;
            }
            else
                toLabel.Text = "N/A";
        }

        private void fromValueTxt1_TextChanged(object sender, EventArgs e)
        {
            convertValue(fromValueTxt1, fromComboBoxExtended1, toComboBoxExtended1, toLabel1);
        }

        private void fromValueTxt2_TextChanged(object sender, EventArgs e)
        {
            convertValue(fromValueTxt2, fromComboBoxExtended2, toComboBoxExtended2, toLabel2);
        }

        private void fromValueTxt3_TextChanged(object sender, EventArgs e)
        {
            convertValue(fromValueTxt3, fromComboBoxExtended3, toComboBoxExtended3, toLabel3);
        }

        private void fromComboBoxExtended1_SelectedIndexChanged(object sender, EventArgs e)
        {
            convertValue(fromValueTxt1, fromComboBoxExtended1, toComboBoxExtended1, toLabel1);
        }

        private void fromComboBoxExtended2_SelectedIndexChanged(object sender, EventArgs e)
        {
            convertValue(fromValueTxt2, fromComboBoxExtended2, toComboBoxExtended2, toLabel2);
        }

        private void fromComboBoxExtended3_SelectedIndexChanged(object sender, EventArgs e)
        {
            convertValue(fromValueTxt3, fromComboBoxExtended3, toComboBoxExtended3, toLabel3);
        }

        private void toComboBoxExtended1_SelectedIndexChanged(object sender, EventArgs e)
        {
            convertValue(fromValueTxt1, fromComboBoxExtended1, toComboBoxExtended1, toLabel1);
        }

        private void toComboBoxExtended2_SelectedIndexChanged(object sender, EventArgs e)
        {
            convertValue(fromValueTxt2, fromComboBoxExtended2, toComboBoxExtended2, toLabel2);
        }

        private void toComboBoxExtended3_SelectedIndexChanged(object sender, EventArgs e)
        {
            convertValue(fromValueTxt3, fromComboBoxExtended3, toComboBoxExtended3, toLabel3);
        }
    }
}
